<?php
namespace app\lucn\controller;

use think\Controller;
use think\Request;
use think\Db;
use fast\lucn\Lucn;
use fast\lucn\Querylist;
/**
 * toutiao控制器
 * @author  Lucn30 <Lucn30@126.com>
 */
class Toutiao extends Controller {
	//protected $server_url = "http://ts6.travian.tw";
	
	public function index(){
	    $ar = '<br><p style="font-size:16px;"><strong><span style="color:#999;">来源：周口新闻</span></strong> </p><p style="text-align:center;"><img class="" width="auto" src="http://mmbiz.qpic.cn/mmbiz_png/IH4SALxDBIyWb2KNzQmLfq3u707NpHtWKOBLP56s2TECIicKkpLsOibzsx75tcO3tTHAUuibrNicj4FBWDtjJ25fMg/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1" style="height:auto !important;width:auto !important;" /> </p>';
	    if($this->request->isPost()){
		    $url = input('param.url');//$url = 'http://127.0.0.1/index.html';
		    $html = file_get_contents($url);
			$preg='/var gallery = ([\s\S]*)var siblingList = /';
			preg_match_all($preg,$html,$array);
			//echo('aaa'.$array[1][0]);die;
			$jsone = substr($array[1][0],0,strlen($array[1][0])-5);
			
			/*$jsone = str_replace('\"','"',$jsone);
			$jsone = str_replace('\/','/',$jsone);
			$jsone = str_replace('\\\\','\\',$jsone);*/
			$jsond = json_decode($jsone,true);
			//dump($jsond);die;
			//echo('aaa'.$jsone);die;
			$htm = '';
			for($i=0;$i<$jsond['count'];$i++){
				$img = $jsond['sub_images'][$i]['url'];
				$htm .= '<p>'.$jsond['sub_abstracts'][$i].'</p>';
				$htm .= '<p><img src="'.$img.'"></p>';
			}
			$htm.= $ar;
			$this->assign('url',$url);
		}else{
		    $htm = $ar;
		}
		$this->assign('html',$htm);
		return $this->Fetch('/toutiao_index');
    }
	
	
    public function format(){
		$url = input('param.url');
		$html = file_get_contents($url);
		//phpQuery::newDocumentFile($this->server_url.'/dorf1.php'); 
		//echo pq(".blkTop h1:eq(0)")->html(); 
		//echo $url;
		//$data = QueryList::getHtml($url);
		//$data = QueryList::Query($url,['link' => ['a','href']])->data;
		//import('fast.lucn.Phpquery');
		//phpQuery::newDocumentFile($url); 
        //echo pq("head")->html(); 
		file_put_contents("s:/test.txt", $html);
        dump($html);
    }
}